package com.inter.controller.insidetask;

import com.inter.entity.common.Partner;
import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.SynchronousPartnerRequestBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.request.subRequest.SynchronousPartnerRequest;
import com.inter.entity.newpojo.response.subResponse.SynchronousPartnerResponse;
import com.inter.entity.newpojo.response.subResponse.common._Partner;
import com.inter.service.PartnerService;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import com.inter.utils.MD5;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 2.0:第二版同步企业接口
 *
 * @author lm
 * @date 2019/07/15
 */

@Configuration
@EnableScheduling
@RestController
public class SyncPartnerController {

    private static Logger logger = Logger.getLogger(SyncPartnerController.class);

    @Autowired
    PartnerService partnerService;

    /**
     * 获取渠道列表
     * 请求体
     *
     * @param
     * @return
     */
    @RequestMapping(value = "synchronousPartner", method = RequestMethod.GET)
    @ResponseBody
    // 每1天执行一次

    //@Scheduled(cron = "0 0 0 1/1 * ?")

    public void syncPartner() {
        try {
            // 请求参数拼接
            SynchronousPartnerRequest request = new SynchronousPartnerRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader("queryPartner", "GMSYS");
            request.setHeader(header);
            SynchronousPartnerRequestBody body = new SynchronousPartnerRequestBody();
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), "wORfzY1DbaB39RJ4s82QxGs8bSGfeU8z");
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SynchronousPartnerRequest.class).toXml(request, "utf-8");
            logger.info("【同步企业】请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("【同步企业】响应：" + result);

            // 将回传的xml转换成object数据

            SynchronousPartnerResponse responseObj = new JaxbUtil(SynchronousPartnerResponse.class).fromXml(result, "utf-8");
            // 返回错误结果的处理
            String retCode = responseObj.getHeader().getResultCode();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                List<_Partner> partnerList = responseObj.getBody().getPartner();

                for (_Partner p : partnerList) {
                    String partnerName = p.getPartnerName();
                    String partnerCode = p.getPartnerCode();
                    String secret = p.getSecret();
                    String token = MD5.calcMD5(partnerName + partnerCode + secret);
                    //先去数据库查询partnerCode是不是已经存在
                    int count = partnerService.findPartnerCodeEixtOrNot(partnerCode);

                    if (count <= 0) {
                        //入库
                        Partner partner = new Partner();
                        partner.setPartnerName(partnerName);
                        partner.setPartnerCode(partnerCode);
                        partner.setSecret(secret);
                        partner.setToken(token);
                        partnerService.insertPartner(partner);

                    } else {
                        continue;
                    }
                }

            }
            logger.info("获取企业列表任务执行完毕！" + retCode);
        } catch (Exception e) {
            logger.info("获取企业列表任务捕获到异常：" + e);
        }
    }

}
